package org.lboot.idmo.module.role;


import lombok.extern.slf4j.Slf4j;
import org.lboot.core.domain.ErrMsg;
import org.lboot.core.exception.BusinessException;
import org.lboot.jpa.jpa.AbstractJpaCurdExt;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SysRoleServiceImpl extends AbstractJpaCurdExt<Long,SysRole,SysRoleDAO> implements SysRoleService {

    @Override
    public List<String> addRolePermissions(String roleId, List<String> perms) {
        return null;
    }

    @Override
    public void createCheck(SysRole t) {
        // 根据条件查询
        List<SysRole> roles = dao.findByRoleNameOrRoleKey(t.getRoleName(), t.getRoleKey());

        if (!roles.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, ErrMsg.build("存在重复角色标识或名称"));
        }
    }

    @Override
    public List<SysRole> getByRoleKeyIn(List<String> roleKeys) {
        return dao.findByRoleKeyIn(roleKeys);
    }

    @Override
    public void updateCheck(SysRole t) {
        // 根据条件查询
        List<SysRole> roles = dao.findByRoleNameOrRoleKey(t.getRoleName(), t.getRoleKey());
        if (roles.isEmpty()){
            throw new BusinessException(HttpStatus.FORBIDDEN,ErrMsg.build("非法方法访问"));
        }
        if (roles.size() == 1){
            SysRole role = roles.get(0);
            //如果ID不相同
            if (!role.getId().equals(t.getId())){
                throw new BusinessException(HttpStatus.BAD_REQUEST,ErrMsg.build("存在重复角色标识或名称"));
            }
            return;
        }
        throw new BusinessException(HttpStatus.BAD_REQUEST,ErrMsg.build("存在重复角色标识或名称"));
    }
}
